import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { ApiProvider } from '../../providers/api/api';

import {joinCourse} from "../../modal/joiuCourse";

import { LocalStorProvider } from '../../providers/local-stor/local-stor';
import {JoinCourseDetailPage} from "../join-course-detail/join-course-detail";

/**
 * Generated class for the JoinPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-join',
  templateUrl: 'join.html',
})
export class JoinPage {

  joinCode;
  studentId;

  constructor(public navCtrl: NavController, public navParams: NavParams, public http: ApiProvider,private storage:LocalStorProvider) {
  }

  ionViewDidLoad() {
  }

  ionViewDidEnter()
  {
    /**
     * 获取当前登录的学生id
     */
    this.storage.getKey("user_id").then(val=>{
      this.studentId=val;
    });
  }

  /**
   * 下一步（根据课程课程号搜索课程）
   */
  searchCourse() {
    this.http.post('/course/searchCourse.do', {classNum:this.joinCode}).subscribe((res:joinCourse) => {
      if(res.Code=="11111111")
      {
        //this.http.errorAlert(res.Message);
        this.http.showToast(res.Message,'bottom');
      }
      else
      {
        this.navCtrl.push(JoinCourseDetailPage,{classcourseId:res.id,studentId:this.studentId,teacherId:+res.teacherId,passwayState:res.passwayState
          ,maxPersonNum:+res.maxPersonNum,courseName:res.className,teacherName:res.teacherName,className:res.class_name,classNum:res.classNum});
      }
    })
  }
}
